package com.jian.mall.produce.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.R;
import com.jian.mall.produce.entity.BrandEntity;
import com.jian.mall.produce.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jian.mall.produce.entity.CategoryBrandRelationEntity;
import com.jian.mall.produce.service.CategoryBrandRelationService;


/**
 * 品牌分类关联
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-08-17 13:22:53
 */
@RestController
@RequestMapping("produce/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */

    /**
     * /product/categorybrandrelation/brands/list
     */
    @GetMapping("/brands/list")
    public R listBrands(@RequestParam(value = "catId",required = false,defaultValue = "0") Long catId) {
        if (catId == 0){
            return R.ok().put("data",categoryBrandRelationService.list());
        }

        List<BrandEntity> brandEntities = categoryBrandRelationService.selectBrandsByCatId(catId);
        List<BrandVo> collect = new ArrayList<>();

        if (brandEntities != null)
            collect = brandEntities.stream().map(i -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(i.getBrandId());
                brandVo.setBrandName(i.getName());
                return brandVo;
            }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }


    @RequestMapping("/list/{brandId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("brandId") Long brandId) {
//        PageUtils page = categoryBrandRelationService.queryPage(params);
        PageUtils page = categoryBrandRelationService.queryPage(params, brandId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
//		categoryBrandRelationService.save(categoryBrandRelation);
        return categoryBrandRelationService.saveById(categoryBrandRelation);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.myUpdateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
